package com.demo.zhlg.controller;

import com.demo.zhlg.mapper.UsersMapper;
import com.demo.zhlg.util.Result;
import com.mybatisflex.core.paginate.Page;
import lombok.Getter;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.demo.zhlg.entity.Users;
import com.demo.zhlg.service.UsersService;
import org.springframework.web.multipart.MultipartFile;
import org.sqlite.date.DateFormatUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  控制层。
 *
 * @author lenovo
 * @since 2025-04-26
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private UsersMapper usersMapper;

    @PostMapping("login")
    public Object login(@RequestBody Users users){
        return usersService.login(users);
    }

    @PostMapping("register")
    public Object register(@RequestBody Users user){
        return usersService.register(user);
    }

    @PostMapping("forgotPassword")
    public Object forgotPassword(@RequestBody Users user) {
        return usersService.forgotPassword(user);
    }

    @GetMapping("checklog")
    public Object checkLog(){
        return usersService.checkLog();
    }

    @GetMapping("getInfo")
    public Object getInfo(){
        return usersService.getInfo();
    }

    @PostMapping("updateInfo")
    public Object updateInfo(@RequestBody Users user){
        user.setUpdateTime(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        return Result.success(usersMapper.update(user));
    }

    @GetMapping("hasLogin")
    public Object hasLogin(){
        return Result.success("");
    }

    @GetMapping("getUserInfo")
    public Object getUserInfo(@RequestParam Integer userId){
        return Result.success(usersService.getById(userId));
    }

    @GetMapping("getPublisherInfo")
    public Object getPublisherInfo(@RequestParam Integer userId){
        return Result.success(usersService.getById(userId));
    }

    @PostMapping(value = "uploadAvatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Object uploadAvatar(@RequestParam("file") MultipartFile file) {
        return usersService.uploadAvatar(file);
    }

    @GetMapping("getAvatar")
    public Object getAvatar(){
        return usersService.getAvatar();
    }

    @GetMapping("getAvatarById")
    public Object getAvatarById(@RequestParam Integer id){
        return usersService.getAvatarById(id);
    }
}
